<?php

use Biz\module\Sys\ErrorConst;
use Biz\service\course\CourseOrder as sCourseOrder;
use Biz\service\trade\Order;
use Biz\service\trade\Pay;
use Doraemon\tools\ActionLog;

class InfoAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REGISTER;

    public function main()
    {

        $orderNo = $this->getRequest()->getQuery('order_no');
        $orderType = $this->getRequest()->getQuery('order_type', Pay::_PAY_ORDER_TYPE_BIZ);
        if (!in_array($orderType, [Pay::_PAY_ORDER_TYPE_BIZ, Pay::_PAY_ORDER_TYPE_COURSE])) {
            throw new \Exception('参数错误');
        }
        $payWays = Pay::getPayWays();
        if ($orderNo) {
            if ($orderType == Pay::_PAY_ORDER_TYPE_BIZ) {
                $orderInfo = Order::getCashierAmount($orderNo);
                if ($orderInfo['uid'] != $this->uid) {
                    throw new \Exception('订单信息异常', ErrorConst::ERROR_PAY_ORDER_INFO_UNKNOW);
                } else {
                    $isPaid = Pay::checkOrderPayStatus($orderNo);
                    if ($isPaid) {
                        throw new \Exception('订单已付款', ErrorConst::ERROR_PAY_ORDER_INFO_PAID);
                    }
                }
                $result = [
                    'summary' => [
                        'total_amount' => $orderInfo['pay_amount'],
                        'total_buynum' => $orderInfo['buy_num']
                    ],
                    'time_info' => [
                        'now_time' => time(),
                        'paystop_time' => $orderInfo['paystop_time'],
                    ],
                    'pay_ways' => $payWays,
                    'reception_group_ids' => $orderInfo['shop_reception_group_ids']
                ];
            } else if ($orderType == Pay::_PAY_ORDER_TYPE_COURSE) {//付费课
                $uid = sCourseOrder::getUidByOrderNo($orderNo);
                if ($uid != $this->uid) {
                    throw new \Exception('订单信息异常', ErrorConst::ERROR_PAY_ORDER_INFO_UNKNOW);
                } else {
                    $isPaid = Pay::checkOrderPayStatus($orderNo, Pay::_PAY_ORDER_TYPE_COURSE);
                    if ($isPaid) {
                        throw new \Exception('订单已付款', ErrorConst::ERROR_PAY_ORDER_INFO_PAID);
                    }
                    $orderInfo = sCourseOrder::getCashierAmount($orderNo);
                    $result = [
                        'summary' => [
                            'total_amount' => $orderInfo['pay_amount'],
                            'total_buynum' => $orderInfo['buy_num']
                        ],
                        'pay_ways' => $payWays,
                    ];
                }
            } else {
                throw new \Exception('订单信息异常', ErrorConst::ERROR_PAY_ORDER_INFO_UNKNOW);
            }
            ActionLog::log($this->uid, ['obj_id' => $orderNo], ActionLog::BIZ_PAY_INFO);
            $this->setSuccess($result);
        }
    }
}